package timing.ukulele.form.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import timing.ukulele.form.persistent.VFormTemplateUnion;
import timing.ukulele.common.ResponseResult;
import timing.ukulele.common.portal.SystemData;
import timing.ukulele.form.client.SystemClient;
import timing.ukulele.form.mapper.VFromTemplateUnionMapper;
import timing.ukulele.form.persistent.FormTemplate;
import timing.ukulele.persistent.service.BaseService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class VFormTemplateUnionService extends BaseService<VFromTemplateUnionMapper, VFormTemplateUnion> {

    @Resource
    private SystemClient systemClient;

    private final FormTemplateService formTemplateService;

    public VFormTemplateUnionService(FormTemplateService formTemplateService) {
        this.formTemplateService = formTemplateService;
    }

    /**
     * 获取分页
     *
     * @param current 当前也页，从1开始
     * @param size    每页大小
     * @return 模板数据
     */
    public IPage<FormTemplate> getItemFormPage(Long systemId, Long templateId, int current, int size) {
        final ResponseResult<SystemData> responseResult = systemClient.getById(systemId);
        Page<FormTemplate> page = new Page<>(current, size);
        LambdaQueryWrapper<FormTemplate> qw = new LambdaQueryWrapper<>();
        Page<VFormTemplateUnion> pageUnion = new Page<>(current, size);
        LambdaQueryWrapper<VFormTemplateUnion> qwUnion = new LambdaQueryWrapper<>();
        if (templateId == null) {
            if (systemId != null) {
                qw.eq(FormTemplate::getSystemId, systemId);
            }
            qw.orderByDesc(FormTemplate::getId);
            page = formTemplateService.getBaseMapper().selectPage(page, qw);
            List<FormTemplate> templates = page.getRecords();
            if (responseResult != null && !Objects.isNull(responseResult.getData())) {
                for (FormTemplate template : templates) {
                    template.setSystemName(responseResult.getData().getName());
                }
            }
            return page;
        } else {
            List<FormTemplate> resultList = new ArrayList<>();
            qwUnion.eq(VFormTemplateUnion::getId, templateId);
            VFormTemplateUnion vFormTemplateUnionUse = getBaseMapper().selectOne(qwUnion);
            qwUnion.clear();
            if (vFormTemplateUnionUse.getUseId() != 0) {
                qwUnion.eq(VFormTemplateUnion::getUseId, vFormTemplateUnionUse.getUseId())
                        .or(wrapper -> wrapper.eq(VFormTemplateUnion::getId, vFormTemplateUnionUse.getUseId())
                                .eq(VFormTemplateUnion::getUseId, 0));
            } else {
                qwUnion.eq(VFormTemplateUnion::getUseId, vFormTemplateUnionUse.getId())
                        .or(wrapper -> wrapper.eq(VFormTemplateUnion::getId, vFormTemplateUnionUse.getId())
                                .eq(VFormTemplateUnion::getUseId, 0));
            }
            pageUnion = getBaseMapper().selectPage(pageUnion, qwUnion);
            pageUnion.getRecords().stream().forEach(e -> {
                FormTemplate formTemplate = new FormTemplate();
                BeanUtils.copyProperties(e, formTemplate);
                formTemplate.setSystemName(responseResult.getData().getName());
                resultList.add(formTemplate);
            });
            page.setTotal(pageUnion.getTotal());
            page.setPages(pageUnion.getPages());
            page.setRecords(resultList);
            return page;
        }
    }
}
